package tmt.usercenter.web.controller.app;

import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import java.util.HashMap;
import java.util.Map;

/**
 * tmt.ss.honghe.mall.controller.app.RestResponseEntityExceptionHandler
 *
 * @author Stark
 * @date 2018/7/19
 */
@ControllerAdvice
public class RestResponseEntityExceptionHandler
		extends ResponseEntityExceptionHandler {

	@ExceptionHandler({AccessDeniedException.class})
	public ResponseEntity<Object> handleAccessDeniedException(Exception ex, WebRequest request) {
		Map<String, String> result = new HashMap<>();
		result.put("message", ex.getMessage());

		return new ResponseEntity<>(result, HttpStatus.FORBIDDEN);
	}

	@ExceptionHandler({InvalidFormatException.class})
	public ResponseEntity<Object> handleInvalidFormatException(Exception ex, WebRequest request) {
		Map<String, String> result = new HashMap<>();
		result.put("message","数据绑定失败" + ex.getMessage());

		return new ResponseEntity<>(result, HttpStatus.BAD_REQUEST);
	}
}